
clear all
set maxvar 30000

use "National YRBS 2023.dta"
  
/* ======================================================== */
/* 	STEP 1: Create Variables & Generate Analytic Sample		*/
/* ======================================================== */

/* Basic Demographics */

* Age 
gen age_final=.
replace age_final = 1 if Q1>=1 & Q1<=3
replace age_final = 2 if Q1>=4 & Q1<=5
replace age_final = 3 if Q1>=6 & Q1<=7
replace age_final = . if Q1==. 

label variable age_final "Age Group"
label define age_final_l 1 "12 and under to 14" 2 "15 to 16" 3 "17+"
label values age_final age_final_l

*Sex
gen sex_male=.
replace sex_male=0 if Q2==1
replace sex_male=1 if Q2==2

label variable  sex_male "Sex Male"
label define sex_male_l 0 "Female" 1 "Male"
label values sex_male sex_male_l 

* Race
gen race_eth_grp=.
replace race_eth_grp=0 if raceeth==3 /* Black */
replace race_eth_grp=1 if raceeth==6 /* Hispanic/Latino */
replace race_eth_grp=2 if raceeth==5 /* White */
replace race_eth_grp=3 if raceeth==2 | raceeth==4 /* Asian/Pacific Islander */
replace race_eth_grp=4 if raceeth==1 /* Am Indian Alaska Native */
replace race_eth_grp=5 if raceeth==7 | raceeth==8 /* Multiple */

label variable  race_eth_grp "Racial/Ethnic Group"
label define race_eth_grp_l 0 "Black" 1 "Latino" 2 "White" ///
	3 "Asian/PI" 4 "Native American" 5 "Multiple"
label values race_eth_grp race_eth_grp_l 

/* Initial sample restrictions: Drop those with missing demographics & SOGI */

drop if Q64==. | Q65==. /* Drops 2166 */

drop if Q1==. | Q2==. | raceeth==. /* Drops 371 */

sum Q1 /* sample size: 17,566 */

/* LGBTQ+ Identification */

gen lgbtq_overall=0
replace lgbtq_overall=1 if inlist(Q64, 2, 3, 4, 5)
replace lgbtq_overall=1 if inlist(Q65, 2, 3)
replace lgbtq_overall=2 if Q64==6 | Q65==4

label variable lgbtq_overall "LGBTQ+ vs. CisHet"
label define lgbtq_overall_l 0 "CisHet" 1 "LGBTQ+" 2 "Don't Understand"
label values lgbtq_overall lgbtq_overall_l


gen lgbq_trans_ind=0
 replace lgbq_trans_ind=0 if Q64==1 & Q65==1
 replace lgbq_trans_ind=1 if inlist(Q64, 2, 3, 4, 5) & Q65==1
 replace lgbq_trans_ind=2 if Q65==2 | Q65==3 
 replace lgbq_trans_ind=3 if Q64==6 | Q65==4

 
label variable lgbq_trans_ind  "Sexual Orientation & Gender Identity"
label define lgbq_trans_ind_l 0 "CisHet" 1 "Cis-LGBQ" 2 "Transgender/Gender Questioning" 3 "Don't Understand"
label values lgbq_trans_ind lgbq_trans_ind_l


/* Process each ACE Item */

gen ace_sex_abuse=.
replace ace_sex_abuse=0 if Q88==2
replace ace_sex_abuse=1 if Q88==1

gen ace_emo_abuse=.
replace ace_emo_abuse=0 if Q89==1
replace ace_emo_abuse=1 if inlist(Q89, 2, 3, 4, 5)

gen ace_phys_abuse=.
replace ace_phys_abuse=0 if Q90==1
replace ace_phys_abuse=1 if inlist(Q90, 2, 3, 4, 5)

gen ace_ipv_inhome=.
replace ace_ipv_inhome=0 if Q91==1
replace ace_ipv_inhome=1 if inlist(Q91, 2, 3, 4, 5)

gen ace_basicneed_notmet=.
replace ace_basicneed_notmet=0 if Q99==5
replace ace_basicneed_notmet=1 if Q99==1 | Q99==2 | Q99==3 | Q99==4 

gen ace_par_subabs=.
replace ace_par_subabs=0 if Q100==2
replace ace_par_subabs=1 if Q100==1

gen ace_par_mentalill=.
replace ace_par_mentalill=0 if Q101==2
replace ace_par_mentalill=1 if Q101==1

gen ace_par_incarc=. 
replace ace_par_incarc=0 if Q102==2
replace ace_par_incarc=1 if Q102==1


/* Only Analyze Complete Cases */

egen total_ace_missing=rowmiss(ace_*)
tab total_ace_missing 			 /* 69% have 0 ACEs missing */
keep if total_ace_missing==0	 /* 5435 observations dropped, leaving 12,131 */


/* Generate Summary ACE Scores */

egen total_count_ace=rowtotal(ace_*), missing

gen ace_category_ind=.
replace ace_category_ind=0 if total_count_ace==0
replace ace_category_ind=1 if total_count_ace==1
replace ace_category_ind=2 if total_count_ace==2 | total_count_ace==3
replace ace_category_ind=3 if total_count_ace>=4 & total_count_ace!=.

gen no_aces=.
replace no_aces=0 if total_count_ace>0 & total_count_ace!=.
replace no_aces=1 if total_count_ace==0

gen one_ace=.
replace one_ace=0 if total_count_ace!=1 & total_count_ace!=.
replace one_ace=1 if total_count_ace==1

gen two_three_ace=.
replace two_three_ace=0 if total_count_ace!=2 & total_count_ace!=3 & total_count_ace!=.
replace two_three_ace=1 if total_count_ace==2 | total_count_ace==3

gen four_more_aces=.
replace four_more_aces=0 if total_count_ace<4 & total_count_ace!=.
replace four_more_aces=1 if total_count_ace>=4


/* Create ID Variable */

gen id=_n

save "2023 National Analytic File.dta", replace


/* ======================================================== */
/* 	STEP 2: Prepare Data for MR Weights Estimation in R		*/
/* ======================================================== */

/*

7 Screening Variables from Cimpian et al. 2023: 
 
Q69             byte    %23.0g     Q69lbl     Fruit eating
Q70             byte    %23.0g     Q70lbl     Green salad eating
Q71             byte    %23.0g     Q71lbl     Potato eating
Q72             byte    %23.0g     Q72lbl     Carrot eating
Q83             byte    %28.0g     Q83lbl     Oral health care
Q6              float   %9.0g                 How tall are you

weight          float   %9.0g                 Overall Analysis Weight

*/

/* Males */

use "2023 National Analytic File.dta", clear

gen cishet=0
replace cishet=1 if lgbtq_overall==0

keep if sex_male==1

keep id cishet  Q69 Q70 Q71 Q72 Q6 weight sex_male

export delimited using "MR Male File for R.csv", replace 

/* Females */
use "2023 National Analytic File.dta", clear

gen cishet=0
replace cishet=1 if lgbtq_overall==0

keep if sex_male==0

keep id cishet Q69 Q70 Q71 Q72 Q6 weight sex_male

export delimited using "MR Female File for R.csv", replace 

/* ======================================================== */
/* 			STEP 3: Generate Weights in R					*/
/* ======================================================== */

/* NOTE: This code, to be run in R, is adopted from Cimpian, Timmer and Kim's (2023) 
   Child Development article. Cimpian, Timmer and Kim's (2023) original code 
   is available here https://osf.io/5xtws/ 										*/


# Males

rm(list=ls()) 
require(randomForest)
require(pROC)
require(twang)
require(survey)
require(gbm)
require(lme4)
require(nlme)
require(data.table)

set.seed(1234)

setwd("") 

sadc=read.csv("MR Male File for R.csv", stringsAsFactors=T) 
attach(sadc)

tableName <- paste("2023_M_Post_TWANG.csv", sep = "")

re <- sadc
renew <- re

# MATCHING FOR MODEL 2 - main/initial screener based on 7 items in lit
ps.mod2 <- ps(cishet ~ Q69 + Q70 + Q71 + Q72 + Q6  + weight,
              data = renew,
              n.trees=15000,
              interaction.depth=3,
              shrinkage=0.01,
              perm.test.iters=0,
              stop.method=c("es.mean","ks.max"),                 
              estimand = "ATT",
              verbose=TRUE)

re$w_es_mean2 <- get.weights(ps.mod2, stop.method="es.mean")
re$w_ks_max2 <- get.weights(ps.mod2, stop.method="ks.max")
re$pses2 <- ps.mod2$ps$es.mean.ATT
re$psks2 <- ps.mod2$ps$ks.max.ATT

summary(ps.mod2)
summary(ps.mod2$gbm.obj,
        n.trees=ps.mod2$desc$ks.max.ATT$n.trees)
plot(ps.mod2, plots = 2)
plot(ps.mod2, plots = 3)
plot(ps.mod2, plots = 4)
plot(ps.mod2, plots = 5)
plot(ps.mod2, plots = 3, subset = 2)

write.table(re, file = tableName,  sep = ",", col.names = NA, qmethod = "double")


# Females 

rm(list=ls()) 
require(randomForest)
require(pROC)
require(twang)
require(survey)
require(gbm)
require(lme4)
require(nlme)
require(data.table)

set.seed(1234)

setwd("") 

sadc=read.csv("MR Female File for R.csv", stringsAsFactors=T) 
attach(sadc)

tableName <- paste("2023_F_Post_TWANG.csv", sep = "")

re <- sadc
renew <- re

# MATCHING FOR MODEL 2 - main/initial screener based on 7 items in lit
ps.mod2 <- ps(cishet ~ Q69 + Q70 + Q71 + Q72 + Q6  + weight,
			data = renew,
              n.trees=15000,
              interaction.depth=3,
              shrinkage=0.01,
              perm.test.iters=0,
              stop.method=c("es.mean","ks.max"),                 
              estimand = "ATT",
              verbose="TRUE")

re$w_es_mean2 <- get.weights(ps.mod2, stop.method="es.mean")
re$w_ks_max2 <- get.weights(ps.mod2, stop.method="ks.max")
re$pses2 <- ps.mod2$ps$es.mean.ATT
re$psks2 <- ps.mod2$ps$ks.max.ATT

summary(ps.mod2)
summary(ps.mod2$gbm.obj,
        n.trees=ps.mod2$desc$ks.max.ATT$n.trees)
plot(ps.mod2, plots = 2)
plot(ps.mod2, plots = 3)
plot(ps.mod2, plots = 4)
plot(ps.mod2, plots = 5)
plot(ps.mod2, plots = 3, subset = 2)

write.table(re, file = tableName,  sep = ",", col.names = NA, qmethod = "double")


/* ======================================================= */
/* 		 STEP 4: Import Weights into Stata & Process       */ 
/* ======================================================= */

/*

NOTE: This code is also adapted from Cimpian, Timmer and Kim's (2023) original code, 
      available here https://osf.io/5xtws/ 	

*/

/*  MALES  */
import delimited using "2023_M_Post_TWANG.csv", clear

gen lgbtq_overall=0
replace lgbtq_overall=1 if cishet==0

gen ipw_ks_max=w_ks_max2*weight

sum ipw_ks_max if lgbtq_overall==1
loc sum_ipw=r(sum)

sum weight if lgbtq_overall==1
loc sum_wgt=r(sum)

replace ipw_ks_max=ipw_ks_max*(`sum_wgt'/`sum_ipw') if lgbtq_overall==1

sum ipw_ks_max

gen ipw_ks_max_1 = weight 

sum ipw_ks_max ipw_ks_max_1 weight

bysort lgbtq_overall: su ipw_*

keep id ipw_* w_* ps*
sort id

save "Male_posttwang_sc_comp.dta", replace

/*  FEMALES  */
import delimited using "2023_F_Post_TWANG.csv", clear

gen lgbtq_overall=0
replace lgbtq_overall=1 if cishet==0

gen ipw_ks_max=w_ks_max2*weight

sum ipw_ks_max if lgbtq_overall==1
loc sum_ipw=r(sum)

sum weight if lgbtq_overall==1
loc sum_wgt=r(sum)

replace ipw_ks_max=ipw_ks_max*(`sum_wgt'/`sum_ipw') if lgbtq_overall==1

sum ipw_ks_max

gen ipw_ks_max_1 = weight 

sum ipw_ks_max ipw_ks_max_1 weight

bysort lgbtq_overall: su ipw_*

keep id ipw_* w_* ps*
sort id
save "Female_posttwang_sc_comp.dta", replace

/*  Combine Male and Female Weights  */

use "Female_posttwang_sc_comp.dta", clear
append using "Male_posttwang_sc_comp.dta"

sort id 
save "Twang Weights Combined.dta", replace

/* Merge onto original Analytic Sample */

use "2023 National Analytic File.dta", clear

sort id 
merge id using "Twang Weights Combined.dta"

tab _merge 
drop _merge 

save "2023 Weighted National Analytic File.dta", replace



/* ======================================================== */
/* 		Table 1: Descriptive Statistics on the Sample		*/
/* ======================================================== */

use "2023 Weighted National Analytic File.dta", clear

svyset [pweight=weight], strata(stratum) psu(psu)  singleunit(scaled)

* Controls: Age, Race, Sex 
	
foreach var of varlist sex_male race_eth_grp age_final {
	
		tab `var', gen(`var'_)
		
	}


svy: mean sex_male_1
	regsave using "Table 1 Sample Description.dta", replace addlabel("Group", "1_Overall")


foreach var of varlist sex_male_2-age_final_3  {
	
		svy: mean `var'
		regsave using "Table 1 Sample Description.dta", append addlabel("Group", "1_Overall")
	
}
	

foreach var of varlist sex_male_1-age_final_3 {
	
		svy, subpop(if lgbtq_overall==0) : mean `var'
			regsave using "Table 1 Sample Description.dta", append addlabel("Group", "2_Cishet")
	
}

foreach var of varlist sex_male_1-age_final_3 {
	
		svy, subpop(if lgbtq_overall==1) : mean `var'
			regsave using "Table 1 Sample Description.dta", append addlabel("Group", "3_LGBTQ")
	
}

foreach var of varlist sex_male_1-age_final_3 {
	
		svy, subpop(if lgbq_trans_ind==1) : mean `var'
			regsave using "Table 1 Sample Description.dta", append addlabel("Group", "4_Cis_LGBQ")
	
}

foreach var of varlist sex_male_1-age_final_3 {
	
		svy, subpop(if lgbq_trans_ind==2) : mean `var'
			regsave using "Table 1 Sample Description.dta", append addlabel("Group", "5_Transgender")
	
}

/* Report Don't Understand Separately */

foreach var of varlist sex_male_1-age_final_3 {
	
		svy, subpop(if lgbtq_overall==2) : mean `var'
			regsave using "Table 1 Sample Description.dta", append addlabel("Group", "6_DontUnderstand")
	
}


/* Manually Collect Sample Sizes and Cell Counts */

tab lgbtq_overall
svy: tab lgbtq_overall 

tab lgbq_trans_ind
svy: tab lgbq_trans_ind 

tab age_final 
tab sex_male 
tab race_eth_grp 

tab age_final lgbtq_overall
tab sex_male lgbtq_overall
tab race_eth_grp lgbtq_overall

tab age_final lgbq_trans_ind if lgbq_trans_ind!=0
tab sex_male lgbq_trans_ind if lgbq_trans_ind!=0
tab race_eth_grp lgbq_trans_ind if lgbq_trans_ind!=0


use "Table 1 Sample Description.dta", clear

gen id=_n

gsort -id

gen rounded=round(coef*100, .1)

gen mean_est=rounded 

keep var  mean_est Group 

reshape wide mean_est, i(var) j(Group) string

gen var_id=.
replace var_id=1 if strpos(var, "age")
replace var_id=2 if strpos(var, "sex")
replace var_id=3 if strpos(var, "race")


sort var_id var 
drop var_id 

save  "Final Table 1 Sample Description.dta", replace


/* ======================================================== */
/* 		Table 2: Descriptive Statistics on ACE Exposure		*/
/* ======================================================== */

use "2023 Weighted National Analytic File.dta", clear

svyset [pweight=ipw_ks_max], strata(stratum) psu(psu) singleunit(centered)

svy: mean ace_phys_abuse 
	regsave using "Table 2 ACE Description.dta", replace addlabel("Group", "0_Overall")


foreach var of varlist  ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces total_count_ace {
	
		svy: mean `var'
		regsave using "Table 2 ACE Description.dta", append addlabel("Group", "0_Overall")
	
}

svy, subpop(if lgbq_trans_ind==0): mean ace_phys_abuse 
	regsave using "Table 2 ACE Description.dta", append addlabel("Group", "1_Cishet")


foreach var of varlist  ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces total_count_ace {
	
		svy, subpop(if lgbq_trans_ind==0): mean `var'
		regsave using "Table 2 ACE Description.dta", append addlabel("Group", "1_Cishet")
	
}
	
foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces total_count_ace {
	
		svy, subpop(if lgbtq_overall==1): mean `var'
		regsave using "Table 2 ACE Description.dta", append addlabel("Group", "2_LGBTQ")
	
}	

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces total_count_ace {
	
		svy, subpop(if lgbq_trans_ind==1): mean `var'
		regsave using "Table 2 ACE Description.dta", append addlabel("Group", "3_Cis_LGBQ")
	
}

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces total_count_ace {
	
		svy, subpop(if lgbq_trans_ind==2): mean `var'
		regsave using "Table 2 ACE Description.dta", append addlabel("Group", "4_Trans")
	
}

/* Report 'Don't Understand Question' Separately */

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces total_count_ace {
	
		svy, subpop(if lgbtq_overall==2): mean `var'
		regsave using "Table 2 ACE Description.dta", append addlabel("Group", "5_DontUnderstand")
	
}

use "Table 2 ACE Description.dta", clear

gen id=_n

gsort -id

gen min_95=coef-(stderr*2)
gen max_95=coef+(stderr*2)

foreach var of varlist coef min_95 max_95{
	
	replace `var'=round(`var'*100, .1) if var!="total_count_ace"
	replace `var'=round(`var', .1) if var=="total_count_ace"
	
}

gen paren_1=" ("
gen comma=", "
gen paren_2=")" 


egen est_full=concat(coef paren_1 min_95 comma max_95 paren_2)

keep est_full var Group 

reshape wide est_full, i(var) j(Group) string


gen var_order=.
replace var_order=1 if var=="ace_phys_abuse"
replace var_order=2 if var=="ace_emo_abuse"
replace var_order=3 if var=="ace_sex_abuse"
replace var_order=4 if var=="ace_basicneed_notmet"
replace var_order=5 if var=="ace_ipv_inhome"
replace var_order=6 if var=="ace_par_subabs"
replace var_order=7 if var=="ace_par_mentalill"
replace var_order=8 if var=="ace_par_incarc"
replace var_order=9 if var=="no_aces"
replace var_order=10 if var=="one_ace"
replace var_order=11 if var=="two_three_ace"
replace var_order=12 if var=="four_more_aces"
replace var_order=13 if var=="total_count_ace"

sort var_order 

save "Final Table 2 ACE Description.dta", replace
 

/* ======================================================== */
/* 		Table 3: Regression-Adjusted RRR					*/
/* ======================================================== */

use "2023 Weighted National Analytic File.dta", clear

svyset [pweight=ipw_ks_max], strata(stratum) psu(psu) singleunit(centered)
 
 /* Overall: LGBTQ+ vs. CisHet */
 
  tab lgbtq_overall, gen(lgbtq_overall_)

 svy: glm  ace_phys_abuse lgbtq_overall_2 lgbtq_overall_3 i.sex_male i.age_final i.race_eth_grp, family(binomial) link(log) eform  level(99.9)
		regsave lgbtq_overall_2 lgbtq_overall_3 using "Table 3 Regression Results.dta", nose ci pval replace level(99.9) addlabel(group, "1_LGBTQ_Overall", outcome, "ace_phys_abuse")

 foreach var of varlist  ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces{
 	
	svy: glm `var' lgbtq_overall_2 lgbtq_overall_3 i.sex_male i.age_final i.race_eth_grp  , family(binomial) link(log) eform   level(99.9)
		regsave lgbtq_overall_2 lgbtq_overall_3 using "Table 3 Regression Results.dta", nose ci pval append level(99.9) addlabel(group, "1_LGBTQ_Overall", outcome, "`var'")

 }
 
 * Emotional Abuse Doesn't Coverge with Binomial, Use Poisson
 	svy: glm ace_emo_abuse lgbtq_overall_2 lgbtq_overall_3 i.sex_male i.age_final i.race_eth_grp  , family(poisson) link(log) eform   level(99.9)
		regsave lgbtq_overall_2 lgbtq_overall_3 using "Table 3 Regression Results.dta", nose ci pval append level(99.9) addlabel(group, "1_LGBTQ_Overall", outcome, "ace_emo_abuse")

 
  /* Subgroups vs. CisHet */

 tab lgbq_trans_ind, gen(lgbq_trans_ind_)
 
 foreach var of varlist ace_phys_abuse  ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces{
 	
	svy: glm `var' lgbq_trans_ind_2 lgbq_trans_ind_3 lgbq_trans_ind_4 i.sex_male i.age_final i.race_eth_grp  , family(binomial) link(log) eform level(99.9)
		regsave lgbq_trans_ind_2 lgbq_trans_ind_3 lgbq_trans_ind_4 using "Table 3 Regression Results.dta", nose ci pval append  level(99.9) ///
			addlabel(group, "2_Subgroups", outcome, "`var'")
	
	}
	
	
 * Emotional Abuse Doesn't Coverge with Binomial, Use Poisson
 	svy: glm ace_emo_abuse lgbq_trans_ind_2 lgbq_trans_ind_3 lgbq_trans_ind_4 i.sex_male i.age_final i.race_eth_grp  , family(poisson) link(log) eform level(99.9)
		regsave lgbq_trans_ind_2 lgbq_trans_ind_3 lgbq_trans_ind_4 ///
		using "Table 3 Regression Results.dta", nose ci pval append level(99.9) addlabel(group, "2_Subgroups", outcome, "ace_emo_abuse")	

 
   
use "Table 3 Regression Results.dta", clear

gen id=_n

gsort -id

gen group_id=0
replace group_id=1 if strpos(var, "overall_2") /* LGBTQ+ Overall */
replace group_id=2 if strpos(var, "ind_2")     /* Cis-SM */
replace group_id=3 if strpos(var, "ind_3")     /* GM */
replace group_id=4 if strpos(var, "overall_3")     /* Don't Know */

drop if strpos(var, "ind_4") /* Don't Need Don't Know Twice */

sort group_id  

gen eform_coef=exp(coef)
gen eform_min_95=exp(ci_lower)
gen eform_max_95=exp(ci_upper)


foreach var of varlist eform_coef eform_min_95 eform_max_95 {
	
	replace `var'=round(`var', .01) 
	
}


gen sig_star=""
replace sig_star="*" if pval<0.00104
replace sig_star="**" if pval<0.000208
replace sig_star="***" if pval<0.0000208


egen est_star=concat(eform_coef sig_star)

gen paren_1=" ("
gen comma=", "
gen paren_2=")" 


egen est_full=concat(est_star paren_1 eform_min_95 comma eform_max_95 paren_2)

keep est_full outcome group_id  

reshape wide est_full, i(outcome) j(group_id) 


gen var_order=.
replace var_order=1 if outcome=="ace_phys_abuse"
replace var_order=2 if outcome=="ace_emo_abuse"
replace var_order=3 if outcome=="ace_sex_abuse"
replace var_order=4 if outcome=="ace_basicneed_notmet"
replace var_order=5 if outcome=="ace_ipv_inhome"
replace var_order=6 if outcome=="ace_par_subabs"
replace var_order=7 if outcome=="ace_par_mentalill"
replace var_order=8 if outcome=="ace_par_incarc"
replace var_order=9 if outcome=="no_aces"
replace var_order=10 if outcome=="one_ace"
replace var_order=11 if outcome=="two_three_ace"
replace var_order=12 if outcome=="four_more_aces"

sort var_order 

save "Final Table 3 ACE Regression.dta", replace



/* ======================================================================================== */
/* 		Figure 1: Regression-Adjusted ACE Scores By Demographic Moderators					*/
/* ======================================================================================== */

use "2023 Weighted National Analytic File.dta", clear

svyset [pweight=ipw_ks_max], strata(stratum) psu(psu) singleunit(centered)

  tab lgbtq_overall, gen(lgbtq_overall_)

 	svy: glm total_count_ace i.lgbtq_overall_2 i.sex_male i.age_final i.race_eth_grp i.lgbtq_overall_3, family(poisson) link(log) eform
		margins i.lgbtq_overall_2, saving("Figure 1 Overall.dta", replace) 
	
	svy: glm total_count_ace i.lgbtq_overall_2##i.sex_male i.age_final i.race_eth_grp i.lgbtq_overall_3, family(poisson) link(log) eform
		margins i.sex_male#i.lgbtq_overall_2, saving("Figure 1 Sex.dta", replace) 
	
	svy: glm total_count_ace i.lgbtq_overall_2##i.race_eth_grp  i.age_final i.sex_male i.lgbtq_overall_3, family(poisson) link(log) eform
		margins i.race_eth_grp#i.lgbtq_overall_2, saving("Figure 1 Race.dta", replace) 

	svy: glm total_count_ace i.lgbtq_overall_2##i.age_final i.race_eth_grp i.sex_male i.lgbtq_overall_3, family(poisson) link(log) eform
		margins i.age_final#i.lgbtq_overall_2, saving("Figure 1 Age.dta", replace) 

	
	
use "Figure 1 Sex.dta", clear

	gen lgbtq=0
	replace lgbtq=1 if _m2==1
	
	gen sex_male=0 
	replace sex_male=1 if _m1==1

	gen model=1

	keep _margin _ci_lb _ci_ub model lgbtq sex_male

	save "Figure 1 Collected.dta", replace
	

use "Figure 1 Race.dta", clear

	gen lgbtq=0
	replace lgbtq=1 if _m2==1
	
	gen race_id=_m1
	
	gen model=2

	keep _margin  _ci_lb _ci_ub model lgbtq race_id 
	
	append using "Figure 1 Collected.dta"
	save "Figure 1 Collected.dta", replace

	

use "Figure 1 Age.dta", clear

	gen lgbtq=0
	replace lgbtq=1 if _m2==1
	
	gen age_id=_m1
	
	gen model=3

	keep _margin  _ci_lb _ci_ub model lgbtq age_id 
	
	append using "Figure 1 Collected.dta"
	save "Figure 1 Collected.dta", replace

	
use "Figure 1 Collected.dta", clear 

gen group_id=.
replace group_id=sex_male if model==1
replace group_id=race_id if model==2
replace group_id=age_id if model==3

sort model group_id lgbtq


gen id=_n 
replace id=id+1 if group_id==1
replace id=id+2 if group_id==2
replace id=id+3 if group_id==3
replace id=id+4 if group_id==4
replace id=id+5 if group_id==5

replace id=id+1 if model==1
replace id=id+6 if model==2
replace id=id+14 if model==3

 

	twoway (bar _margin id if lgbtq==0, fcolor(gs12%40) lcolor(gs8))  (rcap _ci_lb _ci_ub id if lgbtq==0, lcolor(gs8)) ///
		   (bar _margin id if lgbtq==1, fcolor(gs6%40) lcolor(gs6))  (rcap _ci_lb _ci_ub id if lgbtq==1, lcolor(gs6)), ///
		   		legend(size(small) row(1) order(1 "Cishet" 3 "LGBTQ+") position(12) ring(0)) ///
				yscale(range(0 4)) ylabel(0 .5 1 1.5 2 2.5 3 3.5 4, angle(0) labsize(small))  ///
				xline(8.5 29.5, lcolor(gs8) lpattern(solid)) ///
				subtitle("Sex                                  Race/Ethnicity                                           Age       ") ///
			xlabel( 2.5 "F" 5.5 "M" 11.5 "B" 14.5 "L" 17.5 "W" 20.5 "AAPI" 23.5 "AIAN" ///
					26.5 "MU" 32.5 "12-14" 35.5 "15-16" 38.5 "17+", noticks labsize(small) nogrid) ///
					ytitle("Cumulative ACE Score", size(small)) xtitle("", size(small)) ///
					graphregion(color(white) lwidth(thick) lcolor(black)) ///
					saving("Figure 1 ACE Score By Group.gph", replace)

/* Note: Manually change legend position to offset X-axis (-18) Y-axis (-6) */

	graph export "Mittleman_AJPH-202447704R1_F1.pdf", as(pdf) name("Graph") replace


/* Create Corresponding Table With Mean Scores */

use "Figure 1 Collected.dta", clear 

gen mean_ACE_score=round(_margin, .1) 

gen group=.
replace group=1 if sex_male==0
replace group=2 if sex_male==1

replace group=3 if race_id==0
replace group=4 if race_id==1
replace group=5 if race_id==2
replace group=6 if race_id==3
replace group=7 if race_id==4
replace group=8 if race_id==5

replace group=9 if age_id==1
replace group=10 if age_id==2
replace group=11 if age_id==3


keep lgbtq mean_ACE_score group 
reshape wide mean_ACE_score, i(lgbtq) j(group)  

gen lgbtq_s="Cishet"
replace lgbtq_s="LGBTQ+" if lgbtq==1

rename mean_ACE_score1 F
rename mean_ACE_score2 M
rename mean_ACE_score3 B
rename mean_ACE_score4 L
rename mean_ACE_score5 W
rename mean_ACE_score6 AAPI
rename mean_ACE_score7 AIAN
rename mean_ACE_score8 MU
rename mean_ACE_score9 A_12_14
rename mean_ACE_score10 A_15_16
rename mean_ACE_score11 A_17

drop lgbtq 
order lgbtq_s

save "Figure 1 Data Table.dta", replace



/* ========================================================================================================= */
/*								ADDITIONAL RESULTS FOR ONLINE SUPPLEMENT									 */
/* ========================================================================================================= */


/* ========================================================================================================= */
/* 							Online Table 1: Transgender vs. Questioning									     */
/* ========================================================================================================= */

use "2023 Weighted National Analytic File.dta", clear

svyset [pweight=ipw_ks_max], strata(stratum) psu(psu) singleunit(centered)

	svy, subpop(if Q65==2): mean ace_phys_abuse
		regsave using "Online Table 1.dta", replace addlabel("Group", "Trans")
	
	
foreach var of varlist  ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces total_count_ace {
	
		svy, subpop(if Q65==2): mean `var'
		regsave using "Online Table 1.dta", append addlabel("Group", "Trans")
	
}	
	
foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces total_count_ace{
	
		svy, subpop(if Q65==3): mean `var'
		regsave using "Online Table 1.dta", append addlabel("Group", "Questioning")
	
}

* Sample Size & Percentage 

svyset [pweight=weight], strata(stratum) psu(psu) singleunit(centered)

tab Q65 
svy: tab Q65 

use "Online Table 1.dta", clear

gen id=_n

gsort -id

gen min_95=coef-(stderr*2)
gen max_95=coef+(stderr*2)

foreach var of varlist coef min_95 max_95{
	
	replace `var'=round(`var'*100, .1) if var!="total_count_ace"
	replace `var'=round(`var', .1) if var=="total_count_ace"
	
}

gen paren_1=" ("
gen comma=", "
gen paren_2=")" 


egen est_full=concat(coef paren_1 min_95 comma max_95 paren_2)

keep est_full var Group 

reshape wide est_full, i(var) j(Group) string

gen var_order=.
replace var_order=1 if var=="ace_phys_abuse"
replace var_order=2 if var=="ace_emo_abuse"
replace var_order=3 if var=="ace_sex_abuse"
replace var_order=4 if var=="ace_basicneed_notmet"
replace var_order=5 if var=="ace_ipv_inhome"
replace var_order=6 if var=="ace_par_subabs"
replace var_order=7 if var=="ace_par_mentalill"
replace var_order=8 if var=="ace_par_incarc"
replace var_order=9 if var=="no_aces"
replace var_order=10 if var=="one_ace"
replace var_order=11 if var=="two_three_ace"
replace var_order=12 if var=="four_more_aces"
replace var_order=13 if var=="total_count_ace"

sort var_order 
order var est_fullTrans est_fullQuestioning

save "Final Online Table 1.dta", replace
 

/* ========================================================================================================= */
/*						ADDITIONAL RESULTS FOR ONLINE MISCHEVIOUS RESPONDER ANALYSIS						 */
/* ========================================================================================================= */
	
	
	
/* ========================================================================================================= */
/* 						Online Table 2: Comparing Raw and Adjusted Levels of ACEs						     */
/* ========================================================================================================= */

use "2023 Weighted National Analytic File.dta", clear

/* ================== Unscreened, Raw Results ================== */


/* ============ Overall ============ */
svyset [pweight=weight], strata(stratum) psu(psu) singleunit(centered)

	svy, subpop(if lgbtq_overall==1): mean ace_phys_abuse
		regsave using "Online Table 2.dta", replace addlabel("Group", "1_LGBTQ_NS_O")
	
	
foreach var of varlist  ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace {
	
		svy, subpop(if lgbtq_overall==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "1_LGBTQ_NS_O")
	
}	
	
foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "2_Cis_LGBQ_NS_O")
	
}

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==2): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "3_Trans_NS_O")
	
}

/* ============ Male ============ */

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace {
	
		svy, subpop(if lgbtq_overall==1 & sex_male==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "1_LGBTQ_NS_M")
	
}	
	
foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==1 & sex_male==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "2_Cis_LGBQ_NS_M")
	
}

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==2 & sex_male==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "3_Trans_NS_M")
	
}

/* ============ Female ============ */

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace {
	
		svy, subpop(if lgbtq_overall==1 & sex_male==0): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "1_LGBTQ_NS_F")
	
}	
	
foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==1 & sex_male==0): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "2_Cis_LGBQ_NS_F")
	
}

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==2 & sex_male==0): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "3_Trans_NS_F")
	
}


/* ================== Screened, Primary Results for Comparison ================== */

svyset [pweight=ipw_ks_max], strata(stratum) psu(psu) singleunit(centered)
	
/* ============ Overall ============ */

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbtq_overall==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "4_LGBTQ_S_O")
	
}	
	
foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "5_Cis_LGBQ_S_O")
	
}

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==2): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "6_Trans_S_O")
	
}

/* ============ Male ============ */

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbtq_overall==1 & sex_male==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "4_LGBTQ_S_M")
	
}	
	
foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==1 & sex_male==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "5_Cis_LGBQ_S_M")
	
}

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==2 & sex_male==1): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "6_Trans_S_M")
	
}

/* ============ Female ============ */

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbtq_overall==1 & sex_male==0): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "4_LGBTQ_S_F")
	
}	
	
foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==1 & sex_male==0): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "5_Cis_LGBQ_S_F")
	
}

foreach var of varlist ace_phys_abuse ace_emo_abuse ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet total_count_ace{
	
		svy, subpop(if lgbq_trans_ind==2 & sex_male==0): mean `var'
		regsave using "Online Table 2.dta", append addlabel("Group", "6_Trans_S_F")
	
}

use "Online Table 2.dta", clear

gen id=_n

gsort -id

gen est=.
	replace est=round(coef*100, .01) if var!="total_count_ace"
	replace est=round(coef, .01) if var=="total_count_ace"

keep est var Group

reshape wide est, i(var) j(Group) string


gen var_order=.
replace var_order=1 if var=="ace_phys_abuse"
replace var_order=2 if var=="ace_emo_abuse"
replace var_order=3 if var=="ace_sex_abuse"
replace var_order=4 if var=="ace_basicneed_notmet"
replace var_order=5 if var=="ace_ipv_inhome"
replace var_order=6 if var=="ace_par_subabs"
replace var_order=7 if var=="ace_par_mentalill"
replace var_order=8 if var=="ace_par_incarc"
replace var_order=9 if var=="total_count_ace"

sort var_order 

foreach s in O M F{
	
	gen pct_reduction_LGBTQ_`s'=(est1_LGBTQ_NS_`s'-est4_LGBTQ_S_`s')/est1_LGBTQ_NS_`s'
	gen pct_reduction_CisSM_`s'=(est2_Cis_LGBQ_NS_`s'-est5_Cis_LGBQ_S_`s')/est2_Cis_LGBQ_NS_`s'
	gen pct_reduction_Trans_`s'=(est3_Trans_NS_`s'-est6_Trans_S_`s')/est3_Trans_NS_`s'

}

foreach var of varlist pct_reduction_LGBTQ* pct_reduction_CisSM* pct_reduction_Trans*{
	
	replace `var'=round(`var'*100, .01)
	
}

preserve 

keep var *_O
order est1_LGBTQ_NS_O est4_LGBTQ_S_O pct_reduction_LGBTQ_O ///
	  est2_Cis_LGBQ_NS_O est5_Cis_LGBQ_S_O pct_reduction_CisSM_O ///
	  est3_Trans_NS_O est6_Trans_S_O pct_reduction_Trans_O

save "Online Table 2 Overall.dta", replace

restore 

preserve 

keep var *_M
order est1_LGBTQ_NS_M est4_LGBTQ_S_M pct_reduction_LGBTQ_M ///
	  est2_Cis_LGBQ_NS_M est5_Cis_LGBQ_S_M pct_reduction_CisSM_M ///
	  est3_Trans_NS_M est6_Trans_S_M pct_reduction_Trans_M

save "Online Table 2 Male.dta", replace

restore 

preserve 

keep var *_F
order est1_LGBTQ_NS_F est4_LGBTQ_S_F pct_reduction_LGBTQ_F ///
	  est2_Cis_LGBQ_NS_F est5_Cis_LGBQ_S_F pct_reduction_CisSM_F ///
	  est3_Trans_NS_F est6_Trans_S_F pct_reduction_Trans_F

save "Online Table 2 Female.dta", replace

restore 

use "Online Table 2 Male.dta", clear
	gen sex="Male"
	save "Final Online Table 2.dta", replace

use "Online Table 2 Female.dta", clear
	gen sex="Female"
	append using "Final Online Table 2.dta"
	save "Final Online Table 2.dta", replace

use "Online Table 2 Overall.dta", clear
	gen sex="Overall"
	append using "Final Online Table 2.dta"
	save "Final Online Table 2.dta", replace


use "Final Online Table 2.dta", clear

	egen LGBTQ_NS=rowtotal(*LGBTQ_NS*)
	egen LGBTQ_S=rowtotal(*LGBTQ_S*)
	egen pct_reduction_LGBTQ=rowtotal(pct_reduction_LGBTQ_*)
	
	egen CIS_LGBQ_NS=rowtotal(*Cis_LGBQ_NS*)
	egen CIS_LGBQ_S=rowtotal(*Cis_LGBQ_S*)
	egen pct_reduction_CIS_LGBQ=rowtotal(pct_reduction_CisSM_*)
	
	egen TRANS_NS=rowtotal(*Trans_NS*)
	egen TRANS_S=rowtotal(*Trans_S*)
	egen pct_reduction_TRANS=rowtotal(pct_reduction_Trans_*)
	
	
keep var sex LGBTQ_NS-pct_reduction_TRANS

save "Final Online Table 2.dta", replace
	 


	 
/* ========================================================================================================= */
/* 						Online Table 3: Raw, Unadjusted Regression Results								     */
/* ========================================================================================================= */

use "2023 Weighted National Analytic File.dta", clear

svyset [pweight=weight], strata(stratum) psu(psu) singleunit(centered)
 
 /* Overall: LGBTQ+ vs. CisHet */
 
  tab lgbtq_overall, gen(lgbtq_overall_)

 svy: glm  ace_phys_abuse lgbtq_overall_2 lgbtq_overall_3 i.sex_male i.age_final i.race_eth_grp, family(binomial) link(log) eform  level(99.9)
		regsave lgbtq_overall_2 lgbtq_overall_3 using "Online Table 3 Regression Results.dta", nose ci pval replace level(99.9) addlabel(group, "1_LGBTQ_Overall", outcome, "ace_phys_abuse")

 foreach var of varlist  ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces{
 	
	svy: glm `var' lgbtq_overall_2 lgbtq_overall_3 i.sex_male i.age_final i.race_eth_grp  , family(binomial) link(log) eform   level(99.9)
		regsave lgbtq_overall_2 lgbtq_overall_3 using "Online Table 3 Regression Results.dta", nose ci pval append level(99.9) addlabel(group, "1_LGBTQ_Overall", outcome, "`var'")

 }
 
 * Emotional Abuse Doesn't Coverge with Binomial, Use Poisson
 	svy: glm ace_emo_abuse lgbtq_overall_2 lgbtq_overall_3 i.sex_male i.age_final i.race_eth_grp  , family(poisson) link(log) eform   level(99.9)
		regsave lgbtq_overall_2 lgbtq_overall_3 using "Online Table 3 Regression Results.dta", nose ci pval append level(99.9) addlabel(group, "1_LGBTQ_Overall", outcome, "ace_emo_abuse")

 
  /* Subgroups vs. CisHet */

 tab lgbq_trans_ind, gen(lgbq_trans_ind_)
 
 foreach var of varlist ace_phys_abuse  ace_sex_abuse ace_ipv_inhome ace_par_subabs ///
	ace_par_mentalill ace_par_incarc ace_basicneed_notmet no_aces one_ace two_three_ace four_more_aces{
 	
	svy: glm `var' lgbq_trans_ind_2 lgbq_trans_ind_3 lgbq_trans_ind_4 i.sex_male i.age_final i.race_eth_grp  , family(binomial) link(log) eform level(99.9)
		regsave lgbq_trans_ind_2 lgbq_trans_ind_3 lgbq_trans_ind_4 using "Online Table 3 Regression Results.dta", nose ci pval append  level(99.9) ///
			addlabel(group, "2_Subgroups", outcome, "`var'")
	
	}
	
	
 * Emotional Abuse Doesn't Coverge with Binomial, Use Poisson
 	svy: glm ace_emo_abuse lgbq_trans_ind_2 lgbq_trans_ind_3 lgbq_trans_ind_4 i.sex_male i.age_final i.race_eth_grp  , family(poisson) link(log) eform level(99.9)
		regsave lgbq_trans_ind_2 lgbq_trans_ind_3 lgbq_trans_ind_4 ///
		using "Online Table 3 Regression Results.dta", nose ci pval append level(99.9) addlabel(group, "2_Subgroups", outcome, "ace_emo_abuse")	

 
   
use "Online Table 3 Regression Results.dta", clear

gen id=_n

gsort -id

gen group_id=0
replace group_id=1 if strpos(var, "overall_2") /* LGBTQ+ Overall */
replace group_id=2 if strpos(var, "ind_2")     /* Cis-SM */
replace group_id=3 if strpos(var, "ind_3")     /* GM */
replace group_id=4 if strpos(var, "overall_3")     /* Don't Know */

drop if strpos(var, "ind_4") /* Don't Need Don't Know Twice */

sort group_id  

gen eform_coef=exp(coef)
gen eform_min_95=exp(ci_lower)
gen eform_max_95=exp(ci_upper)


foreach var of varlist eform_coef eform_min_95 eform_max_95 {
	
	replace `var'=round(`var', .01) 
	
}


gen sig_star=""
replace sig_star="*" if pval<0.00104
replace sig_star="**" if pval<0.000208
replace sig_star="***" if pval<0.0000208


egen est_star=concat(eform_coef sig_star)

gen paren_1=" ("
gen comma=", "
gen paren_2=")" 


egen est_full=concat(est_star paren_1 eform_min_95 comma eform_max_95 paren_2)

keep est_full outcome group_id  

reshape wide est_full, i(outcome) j(group_id) 


gen var_order=.
replace var_order=1 if outcome=="ace_phys_abuse"
replace var_order=2 if outcome=="ace_emo_abuse"
replace var_order=3 if outcome=="ace_sex_abuse"
replace var_order=4 if outcome=="ace_basicneed_notmet"
replace var_order=5 if outcome=="ace_ipv_inhome"
replace var_order=6 if outcome=="ace_par_subabs"
replace var_order=7 if outcome=="ace_par_mentalill"
replace var_order=8 if outcome=="ace_par_incarc"
replace var_order=9 if outcome=="no_aces"
replace var_order=10 if outcome=="one_ace"
replace var_order=11 if outcome=="two_three_ace"
replace var_order=12 if outcome=="four_more_aces"

sort var_order 

save "Online Final Table 3 ACE Regression.dta", replace

